(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.lookup.loanAmdListController", listController);
	listController.$inject = [ "$scope", "$uibModalInstance", "$filter", "dialogsModule", "gridService", "layoutConfig", "validatorConfig", "authService", "dateService" ];
	function listController($scope, $uibModalInstance, $filter, dialogsModule, gridService, layoutConfig, validatorConfig, authService, dateService) {
		var ctrl = this;
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.searchCriteria = ctrl.searchCriteria || {};

		ctrl.init = function() {
			ctrl.loanAmdGridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.loanAmdGridOptions.data = "vm.searchCriteria.list";
			ctrl.loanAmdGridOptions.multiSelect = false;
			ctrl.amdStatusOptions = [ {
				"value" : "L",
				"label" : "option.amdstatus.l"
			}, {
				"value" : "R",
				"label" : "option.amdstatus.r"
			}, {
				"value" : "X",
				"label" : "option.amdstatus.x"
			} ];
			ctrl.searchCriteria = ctrl.loanAmdGridOptions.defaultSearchCriteria;
			/*if (ctrl.dialogsModule.params.loanId) {
				ctrl.searchCriteria.loanId = ctrl.dialogsModule.params.loanId;
			} else {
				return;
			}*/
			if (ctrl.dialogsModule.params.amdStatus) {
				ctrl.searchCriteria.amdStatus = ctrl.dialogsModule.params.amdStatus;
			}
			ctrl.search();
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				setAmdContect(ctrl.searchCriteria);
			}, angular.noop);
		};

		ctrl.viewAmendment = function(currRow) {
			var params = {};
			params = currRow.entity;
			modalService.openCustomDialogs({
				"title" : "popup.title.amendmentview",
				"dialogsModule" : "cls.loan.amendmentView",
				"params" : params,
				"btnCallback" : {
					"ok" : function(obj) {
					}
				}
			});
		};

		function setAmdContect(searchCriteria) {
			var translate = $filter("translate");
			if (searchCriteria.list) {
				angular.forEach(searchCriteria.list, function(loanAmd, key) {
					var amdCount = 0;
					var contents = "";
					if ("Y" == loanAmd.uMaturityDate) {
						contents += translate("label.loan.maturitydate");
						amdCount++;
					}
					if ("Y" == loanAmd.uBaseRate) {
						if (amdCount > 0)
							contents += ", ";
						contents += translate("label.loan.baserate");
						amdCount++;
					}
					if ("Y" == loanAmd.uSpreadRate || "Y" == loanAmd.uIntReview) {
						if (amdCount > 0)
							contents += ", ";
						contents += translate("label.loan.spreadrate");
						amdCount++;
					}
					if ("Y" == loanAmd.uCofRate) {
						if (amdCount > 0)
							contents += ", ";
						contents += translate("label.loan.cofrate");
						amdCount++;
					}
					if ("Y" == loanAmd.uTaxRate) {
						if (amdCount > 0)
							contents += ", ";
						contents += translate("label.loan.taxrate");
						amdCount++;
					}
					if ("Y" == loanAmd.uInsRate) {
						if (amdCount > 0)
							contents += ", ";
						contents += translate("label.loan.insurancerate");
						amdCount++;
					}
					if ("Y" == loanAmd.uObligorCode) {
						if (amdCount > 0)
							contents += ", ";
						contents += translate("label.loan.obligorcode");
						amdCount++;
					}
					if ("Y" == loanAmd.uObligorInfo) {
						if (amdCount > 0)
							contents += ", ";
						contents += translate("label.loan.obligorinfo");
						amdCount++;
					}
					if ("Y" == loanAmd.uObligorRef) {
						if (amdCount > 0)
							contents += ", ";
						contents += translate("label.loan.obligorref");
						amdCount++;
					}
					if ("Y" == loanAmd.uCustInfo || "Y" == loanAmd.uCustRef) {
						if (amdCount > 0)
							contents += ", ";
						contents += translate("label.loan.custref");
						amdCount++;
					}
					if ("Y" == loanAmd.uPastdue) {
						if (amdCount > 0)
							contents += ", ";
						contents += translate("label.loan.pastdueschedule");
						amdCount++;
					}
					if ("Y" == loanAmd.uBill) {
						if (amdCount > 0)
							contents += ", ";
						contents += translate("label.loan.billingschedule");
						amdCount++;
					}
					if ("Y" == loanAmd.uPerform) {
						if (amdCount > 0)
							contents += ", ";
						contents += translate("label.amendment.perform");
						amdCount++;
					}
					if ("Y" == loanAmd.uLiabAcctCode) {
						if (amdCount > 0)
							contents += ", ";
						contents += translate("label.loan.acctcode");
						amdCount++;
					}
					if ("Y" == loanAmd.uPrincAmt) {
						if (amdCount > 0)
							contents += ", ";
						contents += translate("label.amendment.princamt");
					}
					if (loanAmd.amdComments) {
						if (amdCount > 0)
							contents += ", ";
						contents += translate("label.txn.remarks");
					}
					loanAmd.content = contents;
				});
			}
		}

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : "";
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : "";
			ctrl.search();
		}

		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["close"] || dialogsModule.btnCallback["close"]() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		
		ctrl.ok = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["ok"] || dialogsModule.btnCallback["ok"](ctrl.selectedRows) !== false) {
				$uibModalInstance.close();
			}
		};

	}
})(angular);